package com.workspace.js_website.mapper;

import com.workspace.js_website.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * 用户表相关数据库操作
 */

@Mapper
@Repository("userMapper")
public interface UserMapper {
    @Select("select * from users where username = #{username}")
    User loadUserByUsername(String username);

    @Select("select * from users where mobile = #{mobile}")
    User loadUserByMobile(String mobile);

    @Insert("<script>" +
            "insert into users(user_id, mobile, registration_time, name, username, password, email, department, company) " +
            "values(#{user_id}, #{mobile}, sysdate, #{name}, " +
            "<if test='username!=\"\"'>#{username}, </if>" +
            "<if test='username==\"\"'>null, </if>" +
            "<if test='password!=\"\"'>#{password}, </if>" +
            "<if test='password==\"\"'>null, </if>" +
            "<if test='email!=\"\"'>#{email}, </if>" +
            "<if test='email==\"\"'>null, </if>" +
            "<if test='department!=\"\"'>#{department}, </if>" +
            "<if test='department==\"\"'>null, </if>" +
            "#{company})" +
            "</script>")
    void register(User user);

    @Update("Update users set password = #{newPassword} where user_id = #{userId}")
    void updatePassword(String userId, String newPassword);
}
